import os
import shutil
from tqdm import tqdm


def mkdir(path):
    if not os.path.exists(path):
        os.makedirs(path)


def judge_file_num(dir):
    files = os.listdir(dir)  # 读入文件夹
    num = len(files)
    return num


def copyfile(srcfile, dstpath):  # 复制函数
    if not os.path.isdir(srcfile):
        print("%s not exist!" % (srcfile))
    else:
        fpath, fname = os.path.split(srcfile)  # 分离文件名和路径
        if not os.path.exists(dstpath):
            os.makedirs(dstpath)  # 创建路径
        shutil.copy(srcfile, dstpath + fname)  # 复制文件
        print("copy %s -> %s" % (srcfile, dstpath + fname))


def copydir(srcfile, dstpath):  # 复制函数
    if not os.path.isdir(srcfile):
        print("%s not exist!" % (srcfile))
    else:
        fpath, fname = os.path.split(srcfile)  # 分离文件名和路径
        if not os.path.exists(dstpath):
            os.makedirs(dstpath)  # 创建路径
        dstpath_name = os.path.join(dstpath, fname)
        shutil.copytree(srcfile, dstpath_name)  # 复制文件
        print("copy %s -> %s" % (srcfile, dstpath_name))


def create_calib_label(path):
    mkdir("{}/label/".format(path))
    mkdir("{}/calib/".format(path))


def save_lidar(input_bin_path, file, old_path, add_file):
    # 保存的文件

    copy_bin_name = old_path + '/lidar/' + file
    lidar_path = input_bin_path + '/lidar'
    mkdir(lidar_path)

    shutil.copy(copy_bin_name, lidar_path)  # 复制文件


def save_camera(input_bin_path, file, old_path, add_file):
    # 保存的文件

    camera = add_file + '.png'
    copy_front_name = old_path + '/camera/front/' + camera
    copy_left_name = old_path + '/camera/left/' + camera
    copy_right_name = old_path + '/camera/right/' + camera
    front_path = input_bin_path + '/camera/front'
    left_path = input_bin_path + '/camera/left'
    right_path = input_bin_path + '/camera/right'
    mkdir(front_path)
    mkdir(left_path)
    mkdir(right_path)

    shutil.copy(copy_front_name, front_path)  # 复制文件
    shutil.copy(copy_left_name, left_path)  # 复制文件
    shutil.copy(copy_right_name, right_path)  # 复制文件


def cal_list(path):
    file_list = []
    files = os.listdir(path)
    for file in files:
        file_list.append(file[:-4])
    return file_list


dataname = "/home/wuqi/percetion/roadside_bag/roadside/20230215"
dir_names = os.listdir(dataname)
dir_names = tqdm(dir_names)
new_file = "/home/wuqi/percetion/roadside_bag/roadside/8014"
mkdir(new_file)

for dir_name in dir_names:
    i = 0
    dir_path = dataname + '/' + dir_name + '/' + 'lidar'
    old_path = os.path.dirname(dir_path)
    file_num = judge_file_num(dir_path)
    new_res_calib_label = new_file + '/' + dir_name
    if file_num <= 100:
        old_path = os.path.dirname(dir_path)
        copydir(old_path, new_file)
        create_calib_label(new_res_calib_label)
        print("1")
    else:
        more_bin = new_res_calib_label + '_' + str(i)
        create_calib_label(more_bin)

        lidar_file = os.listdir(dir_path)
        lidar_file.sort()
        # 照片有多少个
        old_camera_path = old_path + '/camera/front'
        camera_list = cal_list(old_camera_path)
        for files in lidar_file:
            add_file = files[:-4]
            create_calib_label(more_bin)

            save_lidar(more_bin, files, old_path, add_file)
            if add_file in camera_list:
                save_camera(more_bin, files, old_path, add_file)
            lidar_num = len(os.listdir(more_bin + '/lidar'))
            if (lidar_num == 100):
                i += 1
                more_bin = new_res_calib_label + '_' + str(i)

print("over")
